<template>
	<view>
		<view>
			<view class="header">
			</view>
			<view class="content">
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像、地区等)</text>
			</view>
			<button class="bottom" type="primary" withCredentials="true" @tap="getUserProfile">授权登录</button>
			<button  type="primary" class='btn' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">使用手机号登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token:'',
				code:'',
				openid:'',
				encryptedData:'',
				iv:'',
			}
		},
		onLoad() {
		},
		methods: {
			
			getPhoneNumber(e) {
				var that = this;
				uni.login({
					success:function(res){
						console.log(res,"unires");
						var data = {
							code:res.code,
							encryptedData:that.encryptedData,
							iv:that.iv,
						}
						that.$http.request({
								url:'login',
								method:'POST',
								header: {
									'Content-Type':'applicktion/json',
								},
								data:data,
							}).then(login =>{
								// uni.hideToast();
								console.log(login,"sigin-login");
							})
						// console.log(res,"wx.log");
					}
				})
			},
			// login(encry,iv){
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success: (res) => {
			// 			console.log(res,"用户信息163215");
			// 			uni.showLoading({
			// 				title: '登录中'
			// 			});
			// 			var data = {
			// 				code:res.code,
			// 				encryptedData:encry,
			// 				iv:iv,
			// 			}
			// 			console.log(data,"data69696969");
			// 			this.$http.request({
			// 				url:'login',
			// 				method:'POST',
			// 				header: {
			// 					'Content-Type':'applicktion/json',
			// 				},
			// 				data:data,
			// 			}).then(login =>{
			// 				uni.hideToast();
			// 				console.log(login,"sigin-login");
			// 			})
						
			// 			//这里获取的是用户的code，用来换取 openid、unionid、session_key 等信息，再将信息丢给后台自己的登录业务就行了
			// 		}
			// 	})
				
			// },
			getUserProfile() {
				uni.getUserProfile({
					desc: '登录',
					success: (info) => {
						console.log(info)
						//这里取到的是用户的信息，自己安排自己的业务
						uni.login({
							provider: 'weixin',
							success: (res) => {
								console.log(res,"用户信息");
								console.log(info,"用户info")
								// uni.showLoading({
								// 	title: '登录中'
								// });
								var arr = [{"avatarUrl":info.userInfo.avatarUrl,"nickName":info.userInfo.nickName,"gender":info.userInfo.gender}];
								var drr = JSON.stringify(arr);
								var data = {
									code:res.code,
									userInfo:drr,
								}
								this.$http.request({
									url:'authorization',
									method:'POST',
									header: {
										'Content-Type':'applicktion/json',
									},
									data:data,
								}).then(login =>{
									console.log(login,"login------6666666666666666666666666666");
									if(login){
										// this.$common.setStorage();
										this.encryptedData = info.encryptedData;
										this.iv = info.iv;
										// this.openid = login.data.data.openid;
										// this.login(info.encryptedData,info.iv);
									}
								})
								
								//这里获取的是用户的code，用来换取 openid、unionid、session_key 等信息，再将信息丢给后台自己的登录业务就行了
							}
						})
					},
					fail: (err) => {
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
